import 'package:boss/models/material.dart';
import 'package:boss/models/package_group.dart';
import 'package:boss/models/sku.dart';
import 'package:boss/models/taste.dart';
import 'package:json_annotation/json_annotation.dart';

part 'commodity.g.dart';

@JsonSerializable(explicitToJson: true)
class Commodity {
  Commodity();

  //普通菜品
  static const int COMMODITY_TYPE_NORMAL = 0;

  //临时菜品
  static const int COMMODITY_TYPE_TEMP = 1;

  //开台必点
  static const int COMMODITY_TYPE_MUST_CHOOSE = 2;

  //套餐
  static const int COMMODITY_TYPE_PACKAGE = 3;

  //称重菜
  static const int COMMODITY_TYPE_WEIGH = 4;

  int id;
  int storeId;
  int menuId;
  String menuName;
  int categoryId;
  String categoryName;
  String title;
  String subTitle;
  String images;
  String ctime;
  String utime;
  int lowestPrice;
  int skuNum;
  int price;
  int originalPrice;
  int memberPrice;
  int buyCount;
  int stock;
  int onsale;
  int status;
  @JsonKey(defaultValue: '')
  String commodityCellTime;
  @JsonKey(defaultValue: '')
  String commodityCellWeek;
  @JsonKey(defaultValue: '')
  String commodityCellDate;
  int commodityMinPie;
  int commodityChefPrint;
  String commodityCode;
  String commodityBarCode;
  String description;
  int commodityDiscountType;
  String unit;
  @JsonKey(defaultValue: [])
  List<Material> commodityMeterialList;
  @JsonKey(defaultValue: 0)
  int meterialType;
  @JsonKey(defaultValue: [])
  List<Taste> commodityTasteList;
  @JsonKey(defaultValue: [])
  List<Sku> commoditySkuList;
  @JsonKey(defaultValue: COMMODITY_TYPE_NORMAL)
  int commodityType;
  @JsonKey(defaultValue: false)
  bool isSelected;
  Sku sku;
  @JsonKey(defaultValue: 0)
  int packaged;
  int costPrice;
  List<PrinterInfo> printerInfoList;
  @JsonKey(defaultValue: 0)
  int count;
  @JsonKey(defaultValue: 0)
  int packagesTag;
  @JsonKey(defaultValue: [])
  List<CommodityFormList> mustCommodityList;
  @JsonKey(defaultValue: [])
  List<PackageGroup> packagesGroupList;
  @JsonKey(name: 'isH5Sell', defaultValue: 1)
  int isH5Sell;

  //是否后厨打印
  bool get isChefPrint => commodityChefPrint == 1;

  //设备id
  List<int> get deviceIds {
    return printerInfoList?.map((item) => item.deviceId)?.toList() ?? [];
  }

  //设备名称
  List<String> get deviceNames {
    return printerInfoList?.map((item) => item.deviceName)?.toList() ?? [];
  }

  //是否仅套餐售卖
  bool get isPackaged => packaged == 1;

  //是否上架售卖
  bool get isOnSale => onsale == 1;

  //是否上架扫码点餐
  bool get isH5 => isH5Sell == 1;

  //是否是称重菜
  bool get isWeighCommodity => commodityType == COMMODITY_TYPE_WEIGH;

  //是否是套餐
  bool get isPackageCommodity => commodityType == COMMODITY_TYPE_PACKAGE;

  //是否是开台必点
  bool get isMustCommodity => commodityType == COMMODITY_TYPE_MUST_CHOOSE;

  //是否是普通菜
  bool get isNormalCommodity => commodityType == COMMODITY_TYPE_NORMAL;

  //临时菜
  bool get isTempCommodity => commodityType == COMMODITY_TYPE_TEMP;

  factory Commodity.fromJson(Map<String, dynamic> json) => _$CommodityFromJson(json);

  Map<String, dynamic> toJson() => _$CommodityToJson(this);
}

@JsonSerializable()
class PrinterInfo {
  String deviceName;

  int deviceId;

  PrinterInfo(
    this.deviceName,
    this.deviceId,
  );

  factory PrinterInfo.fromJson(Map<String, dynamic> srcJson) => _$PrinterInfoFromJson(srcJson);

  Map<String, dynamic> toJson() => _$PrinterInfoToJson(this);
}
